package com.lyncs.ods.utils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONValidator;
import com.lyncs.ods.common.web.RequestHolder;
import com.lyncs.ods.modules.goods.model.SpuDetail;
import com.lyncs.ods.modules.goods.model.SpuInfo;
import org.apache.commons.lang3.StringUtils;

/**
 * @author alex
 * @date 2022/6/11 00:49
 * @description
 */
public class SpuUtils {

    public static String getName(String name, String language, String languageJson) {
        if (StringUtils.isEmpty(language) || StringUtils.isEmpty(languageJson) || !JSONValidator.from(languageJson).validate()) {
            return name;
        }
        JSONObject languageNameJson = JSONObject.parseObject(languageJson);
        return languageNameJson.containsKey(language) ? languageNameJson.getString(language) : name;
    }

    public static String getAttrName(SpuDetail spuDetail) {
        String language = RequestHolder.language();
        if (spuDetail == null) {
            return "";
        }
        return getName(spuDetail.getName(), language, spuDetail.getLanguageJson());
    }

    public static String getName(SpuInfo spuInfo) {
        String language = RequestHolder.language();
        if (spuInfo == null) {
            return "";
        }
        return getName(spuInfo.getName(), language, spuInfo.getLanguageJson());
    }
}
